#include <iostream>
#include <fstream>
#include <list>
#include <sstream>
#include <windows.h>

using namespace::std;

LARGE_INTEGER t1, t2, tc;

int main()
{
    ofstream outfile;
    outfile.open("output.txt", ios::out);
    list<long long int> x[1756];
    ifstream index("ExpIndex",ios::binary);
    int t_count;
    int s_count = 0;
    while(index.read((char*) &t_count, 4))
    {
        long long int tmp;
        for(int i = 0;i < t_count;i++)
        {
            index.read((char*) &tmp, 4);
            x[s_count].push_back(tmp);
        }
        s_count++;
    }
    index.close();
    ifstream query("ExpQuery",ios::in);
    string str;
    while(getline(query,str))
    {
        stringstream s(str);
        int y[6];
        int n = 0;
        string ch;
        while(s >> ch)
        {
            int t = 0;
            for(int i = 0;i < ch.length();i++)
            {
                t = t*10 + ((int)ch[i] - (int)'0');
            }
            y[n] = t;
            n++;
        }
        QueryPerformanceFrequency(&tc);
        QueryPerformanceCounter(&t1);
        list<long long int> tmp = x[y[0]];
        for(int i = 1;i < n;i++)
        {
            for(list<long long int>::const_iterator iter = tmp.begin();iter != tmp.end();iter++)
            {
                int flag = 1;
                for(list<long long int>::const_iterator iter0 = x[y[i]].begin();iter0 != x[y[i]].end();iter0++)
                {
                    if(*iter == *iter0)
                    {
                        flag = 0;
                        break;
                    }
                }
                if(flag == 1)
                {
                    tmp.remove(*iter);
                }
            }
        }
        QueryPerformanceCounter(&t2);
        outfile<<((t2.QuadPart - t1.QuadPart)*1000.0 / tc.QuadPart)<<"ms"<<endl;
    }
    query.close();
    system("pause");
    return 0;
}
